package testcases;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import o.进货单据类型;

import org.junit.Test;

import blservice.commodity.CommodityBLService;
import blservice.document.DocumentBLService;
import server.boot.ServiceLoader;
import vo.AccountBookVo;
import vo.CommodityVo;
import vo.DocumentVo;
import vo.CommodityListItemVo;
import vo.ImportAndReturnDocumentVo;
import client.boot.BLServiceManager;
import db.Database;
import static org.junit.Assert.*;
public class Doc_import_test {
    DocumentBLService bl;   
    CommodityBLService cmbl;
	@Test
	public void test_add() throws SQLException, RemoteException, MalformedURLException, AlreadyBoundException, NotBoundException{
		Database.iniDatabase("localhost", 3306, "seac2", "seac2", "seac2");
		ServiceLoader.iniServiceLoader();
		BLServiceManager.setServerIP("localhost");
	 	AccountBookVo accountbookvo=new AccountBookVo();
	 	accountbookvo.setName("default");
	 	bl = BLServiceManager.load进退货单BLService(accountbookvo); 
	 	ImportAndReturnDocumentVo vo=new ImportAndReturnDocumentVo();
	 	List<CommodityListItemVo> cms=new ArrayList<CommodityListItemVo>();
 	    cms.add(new CommodityListItemVo("cm-00001","atx","777",100,1000,100000,""));
 	    cms.add(new CommodityListItemVo("cm-00002","warrior","610",20,1000,20000,""));
 	    vo.set备注("");
 	    vo.set类型(进货单据类型.进货单);
 	    vo.set总额(120000);
 	    vo.set入库商品列表(cms);
 	    vo.set业务员("ivan");
 	    vo.set仓库("1");
 	    vo.set客户("hello");
	    assertEquals(true,bl.add((DocumentVo)vo).isSuccessed());
	    
	    //添加失败
	    ImportAndReturnDocumentVo vo2=new ImportAndReturnDocumentVo();
	 	List<CommodityListItemVo> cms2=new ArrayList<CommodityListItemVo>();
 	    cms2.add(new CommodityListItemVo("cm-00001","atx","777",100,1000,100000,""));
 	    cms2.add(new CommodityListItemVo("cm-00002","warrior","610",20,1000,20000,""));
 	    vo2.set备注("");
 	    vo2.set类型(进货单据类型.进货单);
 	    vo2.set总额(120000);
 	    vo2.set客户(null);
 	    vo2.set入库商品列表(cms);
 	    vo2.set业务员("ivan");
	    assertEquals("请完整单据！",bl.add((DocumentVo)vo2).getFailMessage());
	    
	 }
	
	@Test
	public void test_getUnapproval() throws MalformedURLException, RemoteException, NotBoundException, SQLException{
		BLServiceManager.setServerIP("localhost");
	 	AccountBookVo accountbookvo=new AccountBookVo();
	 	accountbookvo.setName("default");
	 	bl = BLServiceManager.load进退货单BLService(accountbookvo); 
	 	List<DocumentVo> list_exp=new ArrayList<DocumentVo>();
	 	DocumentVo v1=new DocumentVo();v1.set单据编号("JHD-20141223-00001");
	 	list_exp.add(v1);
	 	DocumentVo v2=new DocumentVo();v2.set单据编号("JHD-20141231-00002");
	 	list_exp.add(v2);
	 	List<DocumentVo> list=bl.getUnapproval();
	 	for(int i=0;i<list.size();i++){
	 		assertEquals(list_exp.get(i).get单据编号(),list.get(i).get单据编号());
	 	}
	}
	@Test
	public void test_getFrom_to() throws MalformedURLException, RemoteException, NotBoundException, SQLException, ParseException{
		BLServiceManager.setServerIP("localhost");
	 	AccountBookVo accountbookvo=new AccountBookVo();
	 	accountbookvo.setName("default");
	 	bl = BLServiceManager.load进退货单BLService(accountbookvo); 
	 	List<DocumentVo> list_exp=new ArrayList<DocumentVo>();
	 	DocumentVo v1=new DocumentVo();v1.set单据编号("JHD-20141223-00001");
	 	list_exp.add(v1);
	 	DocumentVo v2=new DocumentVo();v2.set单据编号("JHD-20141231-00002");
	 	list_exp.add(v2);
	 	DocumentVo from=new DocumentVo();DocumentVo to=new DocumentVo();
	 	from.setDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-11-20"));
	 	to.setDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-11-30"));
		List<DocumentVo> list=bl.get(from,to);
		assertEquals(0,list.size());
		
		DocumentVo from1=new DocumentVo();DocumentVo to1=new DocumentVo();
	 	from1.setDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-12-20"));
	 	to1.setDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-12-30"));
		List<DocumentVo> list2=bl.get(from1,to1);
		for(int i=0;i<list2.size();i++){
	 		assertEquals("JHD-20141223-00001",list2.get(i).get单据编号());
	 	}
		DocumentVo from2=new DocumentVo();DocumentVo to2=new DocumentVo();
	 	from2.setDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-12-20"));
	 	to2.setDate(new SimpleDateFormat("yyyy-MM-dd").parse("2014-12-30"));
		List<DocumentVo> list3=bl.get(from2,to2);
		for(int i=0;i<list3.size();i++){
	 		assertEquals(list_exp.get(i).get单据编号(),list3.get(i).get单据编号());
	 	}
		
	}
	@Test
	public void test_approve() throws MalformedURLException, RemoteException, NotBoundException, SQLException{
		BLServiceManager.setServerIP("localhost");
	 	AccountBookVo accountbookvo=new AccountBookVo();
	 	accountbookvo.setName("default");
	 	bl = BLServiceManager.load进退货单BLService(accountbookvo);
	 	cmbl=BLServiceManager.getCommodityBLService(accountbookvo);
	 	//审批已审批报错
	 	DocumentVo v1=new DocumentVo();v1.set单据编号("JHD-20141223-00001");
	 	assertEquals("该单据已审批！",bl.approve(v1).getFailMessage());
	 	//审批成功查看库存
	 	ImportAndReturnDocumentVo v2=new ImportAndReturnDocumentVo();v2.set单据编号("JHD-20141231-00002");
	 	List<CommodityListItemVo> cm=new ArrayList<CommodityListItemVo>();
 	    cm.add(new CommodityListItemVo("cm-00001","atx","777",100,1000,100000,""));
 	    cm.add(new CommodityListItemVo("cm-00002","warrior","610",20,1000,20000,""));
 	    v2.set备注("");
 	    v2.set类型(进货单据类型.进货单);
 	    v2.set总额(120000);
 	    v2.set入库商品列表(cm);
 	    v2.set业务员("ivan");
 	    v2.set仓库("1");
 	    v2.set客户("hello");
	 	List<CommodityVo> cms=cmbl.getAll();
	 	List<Integer> stockNum=new ArrayList<Integer>();
	    for(int k=0;k<cm.size();k++)
	 	for(int i=0;i<cms.size();i++){
	    	if(cms.get(i).get编号().equals(cm.get(k).get商品编号())){
	    		stockNum.add(cms.get(i).getStocknum());
	    		
	    	}
	    }
	    assertEquals(true,bl.approve(v2).isSuccessed());
	 	List<Integer> stockNum2=new ArrayList<Integer>();
	    for(int k=0;k<cm.size();k++)
	 	for(int i=0;i<cms.size();i++){
	    	if(cms.get(i).get编号().equals(cm.get(k).get商品编号())){
	    		stockNum2.add(cms.get(i).getStocknum());
	    		
	    	}
	    }
	 	for(int i=0;i<cm.size();i++){
	 		assertTrue(stockNum.get(i)+cm.get(i).get数量()==stockNum2.get(i));
	 	}
	 }
	
	
}
